package com.example.datamanage.service.impl;

import com.example.datamanage.common.Result;
import com.example.datamanage.mapper.DataMapper;
import com.example.datamanage.service.DataService;
import com.example.datamanage.utils.ExportUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 28142
 * @description 数据查询 service 接口实现类
 * @date 2023/7/26 17:28
 */
@Service
public class DataServiceImpl implements DataService {
    @Autowired
    private DataMapper dataMapper;
    @Autowired
    private ExportUtils exportUtils;


    @Override
    public Result showTables(String type, String database) {
        List<LinkedHashMap<String, String>> list = null;
        if ("mysql".equalsIgnoreCase(type)) {
             list = dataMapper.showTables(database);
        } else if ("sqlserver".equalsIgnoreCase(type)) {
            list = dataMapper.showTablesMS(database);
        }
        return Result.success(list);
    }

    @Override
    public Result showViews(String type, String database) {
        List<LinkedHashMap<String, String>> list = null;
        if ("mysql".equalsIgnoreCase(type)) {
            list = dataMapper.showViews(database);
        } else if ("sqlserver".equalsIgnoreCase(type)) {
            list = dataMapper.showViewsMS(database);
        }
        return Result.success(list);
    }

    @Override
    public Result showAllTables(String database) {
        return Result.success(dataMapper.showAllTables(database));
    }

    @Override
    public Result showColumns(String database, String table) {
        // 返回结果
        Map<String, Object> map = new HashMap<>();
        // 字段详情
        List<LinkedHashMap<String, String>> list =  dataMapper.showColumns(database, table);
        map.put("detail", list);
        // 字段数
        int colLen = dataMapper.getColLen(database, table);
        map.put("colLen", colLen);
        // 记录数
        int recordsLen = dataMapper.getRecordsLen(database, table);
        // 单位为万，使用DecimalFormat保留四位小数
        DecimalFormat decimalFormat = new DecimalFormat("0.0000");
        String format = decimalFormat.format(recordsLen / 10000.0);
        map.put("recordsLen", format);
        return Result.success(map);
    }

    @Override
    public Result showTablesByCondition(String database, Integer pageNum, Integer pageSize, String chName, String enName) {
        Map map = new HashMap();
        // 计算起始行
        Integer start = (pageNum - 1) * pageSize;
        map.put("total", dataMapper.showTablesNumByCondition(database, chName, enName));
        map.put("tables", dataMapper.showTablesByCondition(database, chName, enName, start, pageSize));
        return Result.success(map);
    }

    @Override
    public void fullExport(String table, String fileName, String type, List<String> fields, HttpServletResponse response) {
        // 查询结果
        List<LinkedHashMap<String, Object>> list = dataMapper.exportData(table, fields);
        // 导出
        exportUtils.export(fileName, type, list, response);
    }

    @Override
    public Result getInfo(String database, String type, Integer pageNum, Integer pageSize) {
        Result result = showTablesByCondition(database, pageNum, pageSize, null, null);
        Map map = (Map) result.getData();
        if ("mysql".equalsIgnoreCase(type)) {
            map.put("tablesNum", dataMapper.getTablesNum(database));
            map.put("viewsNum", dataMapper.getViewsNum(database));
        } else if ("sqlserver".equalsIgnoreCase(type)) {
            map.put("tablesNum", dataMapper.getTablesNumMS(database));
            map.put("viewsNum", dataMapper.getViewsNumMS(database));
        }

        return Result.success(map);
    }
}
